<template>
  <div id="app">
    <transition :name="transitionName" :mode="modeName">
      <navigation>
        <router-view></router-view>
      </navigation>
    </transition>
    <UpdatePop v-if="$store.state.updatePop"></UpdatePop>
  </div>
</template>

<script>
// import "@/assets/js/back.js";
import { Dialog, Toast } from "vant";
// import Services from "@/service/index.js";// deling 99
// import axios from "axios";// deling 99
import UpdatePop from "@/components/updatePop";
export default {
  name: "App",
  data() {
    return {
      loginToken: "",
      transitionName: "router-fade",
      modeName: "out-in"
      // updatePop: true
    };
  },
  components: { UpdatePop },
  watch: {
    $route: function(to, from) {
      if (to.path) {
        this.$store.state.updatePop = false;
      }
      // deling00
      //    console.log(this.$route);
      //    console.log("页面路由to=" + to.name + "++++from=" + from.name);
      // ios交互处理
      //    console.log("是否是IOSAPP++▼");
      //    console.log(this.$route.fullPath.indexOf("fromIOS=ios") > -1);
      this.iosInput();
      // 需要登录的页面统一处理
      // if (this.$route.meta.needLogin) {
      //   Services.getUser(this.loginToken).then(res => {
      //     if (res.code == "0000") {
      //       // 登录
      //       this.userObj = res.user;
      //       localStorage.setItem("token", res.token);
      //       localStorage.setItem("userObj", JSON.stringify(this.userObj));
      //     } else if (res.code == "9112") {
      //       // 登录失效
      //       Toast("登录失效，请重新登录");
      //     } else {
      //       Toast(res.msg);
      //       localStorage.clear();
      //       sessionStorage.clear();
      //     }
      //   });
      // }
      // // 百度统计
      // if (to.path) {
      //   _hmt.push(["_trackPageview", "/#" + to.fullPath]);
      // }
      // // cnzz统计
      // if (window._czc) {
      //   let location = window.location;
      //   let contentUrl = location.pathname + location.hash;
      //   let refererUrl = "/";
      //   window._czc.push(["_trackPageview", contentUrl, refererUrl]);
      // }
      // if (to.name == "home") {
      //   this.activityPop = true;
      // }
    }
  },
  mounted() {
    var that_ = this;
    document.addEventListener("plusready", function() {
      plus.navigator.setStatusBarBackground("#fff");
      plus.navigator.setStatusBarStyle("dark");
      var webview = plus.webview.currentWebview();
      var first = null;
      var first1 = null;
      plus.key.addEventListener("backbutton", function() {
        if (
          that_.$route.name == "home" ||
          that_.$route.name == "news" ||
          that_.$route.name == "assets" ||
          that_.$route.name == "quotation" ||
          that_.$route.name == "mine"
        ) {
          history.pushState(null, null, location.href);
          if (!first1) {
            first1 = new Date().getTime();
            Toast(that_.$t("m.logoutApp"));
            setTimeout(function() {
              first1 = null;
            }, 2000);
          } else {
            plus.runtime.quit();
          }
          return false;
        }
        webview.canBack(function(e) {
          if (e.canBack) {
            // webview.back();
            // that_.$router.go(-1);
            // Toast("子页面");
          } else {
            //首页返回键处理  没有历史记录了
            //处理逻辑：1秒内，连续两次按返回键，则退出应用；
            if (!first) {
              first = new Date().getTime();
              Toast(that_.$t("m.logoutApp"));
              setTimeout(function() {
                first = null;
              }, 1000);
            } else {
              plus.key.addEventListener(
                "backbutton",
                function() {
                  if (new Date().getTime() - first < 1500) {
                    plus.runtime.quit();
                  }
                },
                false
              );
            }
            // _____________________
            // plus.key.addEventListener(
            //   "backbutton",
            //   function() {
            //     //首次按键，提示‘再按一次退出应用’
            //     if (!first) {
            //       first = new Date().getTime();
            //       Toast("再按退出");
            //       // Toast(that_.$t("m.logoutApp"));
            //       setTimeout(function() {
            //         first = null;
            //       }, 1000);
            //     } else {
            //       if (new Date().getTime() - first < 1500) {
            //         plus.runtime.quit();
            //       }
            //     }
            //   },
            //   false
            // );
          }
        });
      });
    });
  },
  created() {
    this.loginToken = localStorage.getItem("token")
      ? localStorage.getItem("token")
      : "";
  },
  updated() {},
  computed: {},
  methods: {
    // 检查是否有新的版本
    updateApp() {
      // if(!!localStorage.getItem('version'))
    },
    // ios12 软键盘问题
    iosInput() {
      var u = navigator.userAgent;
      var flag;
      var myFunction;
      var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      if (isIOS) {
        document.body.addEventListener("focusin", () => {
          //软键盘弹起事件
          flag = true;
          clearTimeout(myFunction);
        });
        document.body.addEventListener("focusout", () => {
          //软键盘关闭事件
          flag = false;
          if (!flag) {
            myFunction = setTimeout(function() {
              window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); //重点  =======当键盘收起的时候让页面回到原始位置(这里的top可以根据你们个人的需求改变，并不一定要回到页面顶部)
            }, 200);
          } else {
            return;
          }
        });
      } else {
        return;
      }
    }
  }
};
</script>

<style lang='scss'>
@import "./assets/css/common.scss";
// @import "./assets/font/iconfont.css";
#app {
  width: 100%;
  height: 100%;
  visibility: hidden;
  color: #40404c;
  z-index: 2;
  position: absolute;
  .rootMain {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: calc(100% - 1rem);
    overflow: hidden;
    background: #fefefe;
    -webkit-overflow-scrolling: touch;
  }
  .btn {
    margin: 0 auto;
    margin-top: 0.3rem;
    width: 80%;
    height: 0.85rem;
    line-height: 0.85rem;
    font-size: 0.32rem;
    letter-spacing: 0.05rem;
    text-align: center;
    border-radius: 0.1rem;
    color: #fff;
    background: #595866;
    &.btn-lg {
      background: linear-gradient(to right, #43434e, #727181);
    }
  }
  .red {
    color: #fd6262;
  }
  .topImg {
    height: 4.64rem;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    flex-direction: column;
    padding-top: 0.35rem;
    background: url("~@/assets/img/topbg.png") no-repeat center / 100% 100%;
    > div {
      width: 3.12rem;
      height: 3.12rem;
    }
    p {
      display: inline-block;
      margin-bottom: 0.2rem;
      color: #fff;
      background-image: -webkit-gradient(
        linear,
        0 0,
        0 bottom,
        from(rgba(255, 181, 25, 1)),
        to(rgba(239, 223, 109, 1))
      );
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }
  // end
  // 弹窗
  .tempPop {
    background: rgba(0, 0, 0, 0.3);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 999;
    display: flex;
    > div {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 70%;
      // height: 100%;
      border-radius: 0.2rem;
      overflow: hidden;
      transform: translateY(-50%) translateX(-50%);
      .tempPopImg {
        img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
        div {
          position: absolute;
          bottom: 2rem;
          left: 0;
          width: 100%;
          text-align: center;
          a {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0 auto;
            background: linear-gradient(right, #ecca96, #d0a060);
            width: 70%;
            height: 0.75rem;
            border-radius: 0.06rem;
            color: #fff;
            font-size: 0.28rem;
            letter-spacing: 0.02rem;
            text-indent: 0.02rem;
          }
        }
      }
      .tempPopClose {
        width: 0.47rem;
        height: 0.46rem;
        position: absolute;
        top: 0rem;
        right: 0;
        img {
          width: 0.37rem;
          height: 0.36rem;
          overflow: hidden;
          position: absolute;
          top: 0.076rem;
          transform: scale(0.5);
          right: 0.07rem;
          object-fit: contain;
        }
      }
    }
  }
  // 行情
  .line-up {
    color: #03ad8f !important;
  }
  .line-down {
    color: #d14b64 !important;
  }
  .bg-up {
    background-color: #03ad8f !important;
  }
  .bg-down {
    background-color: #d14b64 !important;
  }
}
</style>